import java.util.Stack;
//逆波兰表达式求值

public class Solution {
    public int evalRPN(String[] tokens) {
        //首先需要将字符串转化为整型数字
        //遍历字符串数组
        Stack<Integer> stack = new Stack<>();
        for (String s : tokens) {
            //判断该字符串是数字还是运算符号
            if (isNumber(s)) {
                //数字,将其压入栈桢内
                Integer val = Integer.valueOf(s);
                stack.push(val);
            } else {
                //如果是运算符号，先将靠近栈顶的两个数取出来进行运算
                Integer val2 = stack.pop();
                Integer val1 = stack.pop();
                switch (s) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        int sum = stack.pop();
        return sum;
    }

    public boolean isNumber(String val) {
        if (val.equals("+") || val.equals("-") || val.equals("*") || val.equals("/")) {
            return false;
        }
        return true;
    }
}
